﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace nkl
{

    public class BagScript : MonoBehaviour
    {

        public GameObject itemUi;

        public GameObject bagNode;

        List<Slot> baglots;

        // Start is called before the first frame update
        void Start()
        {
            /// 获取所有背包格子
            baglots = findSlots(bagNode.transform);
            /// 拿数据填充背包
            this.fillBags();
        }


        void fillBags()
        {

            ///遍历数据库
            List<HeroBagItem> items = ResourceManager.Instance.heroBag.dataItems;

            /// 如果空格子小于物品数，说明背包满了
            if (baglots.Count < items.Count)
            {
                return;
            }

            for (int i = 0; i < baglots.Count; i++)
            {
                Slot slotItem = baglots[i];
                if (i < items.Count)
                {
                    HeroBagItem item = items[i];
                    slotItem.itemCountText.text = item.count.ToString();
                    slotItem.itemCountText.enabled = true;
                    slotItem.type = item.type;
                }
            }
        }



        List<Slot> findSlots(Transform parentTransform)
        {
            List<Slot> slots = new List<Slot>();

            foreach(Transform tp in parentTransform)
            {
                
                if(tp.tag == "Slot")
                {
                    Slot slot = tp.GetComponent<Slot>();
                    slots.Add(slot);
                }
            }

            //slots.Sort((Comparison<Slot>)delegate (Slot a, Slot b) { return a.index > b.index ? 1 : a == b ? 0 : -1; });

            return slots;

        }
    }
}